package com.app.service;

import java.util.Calendar;

import com.app.alarm.R;
import com.app.model.AlarmDataManager;
import com.app.model.AlarmDataManager.AlarmLoader;
import com.app.util.Util;
import com.app.util.VolumeManager;
import com.app.view_controller.AlarmRecevierActivity;
import com.app.view_controller.ListFragment;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.RemoteViews;

public class HandleAlarmNotificationService extends IntentService {
	private final static boolean DEBUG = true;
	private final static String TAG = "HANDLE_ALARM_NOTIFICATION_SERVICE";
	public static final String START_NOTIFICATION = "start notification";
	public static final String ALARM_DATA = "alarm data";
	public static final int ALARM_SET_NOTIFICATION_ID = 2;
	private static final String CANCEL_ALARM = "stop alarm";
	private static final String ACTION_CLOSE_ACTIVITY = "close activity";

	public HandleAlarmNotificationService() {
		super("HandleAlarmNotificationService");
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		String action = intent.getAction();
		if (action != null && START_NOTIFICATION.equals(action)) {

			//			Intent startActivityIntent = new Intent(getActivity(), AlarmService.class);
			//			startActivityIntent.setAction(AlarmRecevierActivity.START_ACTIVITY);
			//			PendingIntent startActivityPendingIntent = PendingIntent.getService(this, 0, startActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
			//TODO Add Notification Intent
			
			//Intent for 
			Intent stopIntent = new Intent(this, HandleAlarmNotificationService.class);
			stopIntent.setAction(CANCEL_ALARM);
			stopIntent.putExtra(ALARM_DATA, intent.getStringExtra(ALARM_DATA));

			AlarmDataManager alarm = AlarmLoader.getFromStorage(intent.getStringExtra(ALARM_DATA), this);
			String title = getString(R.string.handle_notification_title, Util.getAlarmTimeString(alarm.getCalendarAlarm()));
			String text = null;
			if (alarm.isControlRingtoneEneable() && alarm.isControlNotificationEneable())
				text = String.format(getResources().getQuantityString(R.plurals.handle_notification_text, 2), getString(R.string.ringotone),
						getString(R.string.notifications));
			else {
				if (alarm.isControlRingtoneEneable())
					text = String.format(getResources().getQuantityString(R.plurals.handle_notification_text, 1), getString(R.string.ringotone));
				if (alarm.isControlNotificationEneable()) {
					text = String.format(getResources().getQuantityString(R.plurals.handle_notification_text, 1), getString(R.string.notifications));
				}
			}

			PendingIntent stopPendingIntent = PendingIntent.getService(this, 0, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT);
			NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.alarm_icon).setContentTitle(title)
					.setContentText(text).addAction(android.R.drawable.ic_menu_close_clear_cancel, getString(R.string.Cancel_alarm), stopPendingIntent);

			NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
			Notification notification = builder.build();
			mNotificationManager.notify(ALARM_SET_NOTIFICATION_ID, notification);
		}

		if (action != null && CANCEL_ALARM.equals(action)) {
			if (DEBUG)
				Log.d(TAG, "cancel alarm called");
			AlarmDataManager alarm = AlarmLoader.getFromStorage(intent.getStringExtra(ALARM_DATA), this);
			VolumeManager.getVolumeManagerInstance(this).restoreDefaultVolumeLevel(VolumeManager.STREAM_NOTIFICATION);
			VolumeManager.getVolumeManagerInstance(this).restoreDefaultVolumeLevel(VolumeManager.STREAM_RING);
			alarm.removeAlarm();
			alarm.savetoStorage();
			NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
			mNotificationManager.cancel(ALARM_SET_NOTIFICATION_ID);
			Intent updateListIntent = new Intent(ListFragment.ACTION_UPDATE_LIST);
			sendBroadcast(updateListIntent);
		}

	}

	private String getString(String quantityString, String string) {
		// TODO Auto-generated method stub
		return null;
	}

}
